<?php
namespace Apipad\Controller;
use Think\Controller;

class UserTuanDeptController extends ComController{


    public function index(){
        $detail = I('');
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:10;
        $kewords=$detail['kewords'];
        $model = D('UserTuanDept');
        $map = [];
        if(I('keyword')){
            $map['title|number'] = array('like','%'.I('keyword').'%');
        }
        $map['uid'] = UID;
        $map['tid'] = $detail['tid'];
        $map['is_delete'] = 0;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page,$limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

     //编辑
     public function edit(){
        $detail = I('');
        $id = $detail['id'];
        $title = $detail['title'];
        $number = $detail['number'];
        if(empty($id)){
            $data['code'] = 0;
            $data['msg'] = '部门ID不能为空';
            exit(json_encode($data));
        }
        if(empty($title)){
            $data['code'] = 0;
            $data['msg'] = '请填写部门名称';
            exit(json_encode($data));
        }
        if(empty($number)){
            $data['code'] = 0;
            $data['msg'] = '请填写人数';
            exit(json_encode($data));
        }
        $model = D('UserTuanDept');
        $s_data['title'] = $title;
        $s_data['number'] = $number;
        $result = $model->where(['id'=>$id,'uid'=>UID])->save($s_data);
        $info = $model->where(['id'=>$id,'uid'=>UID])->find();
        if($result){
            $data['code'] = 200;
            $data['msg'] = '编辑成功';
            $data['data'] = $info;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '编辑失败';
            exit(json_encode($data));
        }
    }



    function _before_add(){
        if(!I('tid')){
            $this->ajaxErrReturn('团体基本信息必须');
        }
        $model = M('user_tuan');
        $wdata['id'] = I('tid');
        $wdata['uid'] = UID;
        $vo = $model->where($wdata)->find();
        if(!$vo){
            $this->ajaxErrReturn('团体基本信息数据不存在');
        }
    
    }

    //删除后置
    function _after_vdel($id){
        $model = M('user_tuan_detail');
        $wdata['did'] = $id;
        $sdata['is_delete'] = 1;
        $model->where($wdata)->save($sdata);
    }

}